using imitateMacQQ.WebApi.Domain.Login.register.Login.Interface;
using imitateMacQQ.WebApi.Domain.Login.register.Login.letter;
using imitateMacQQ.WebApi.Repository.EFCore.Model;
using imitateMacQQ.WebApi.Repository.EFCore.transform;

namespace imitateMacQQ.WebApi.Domain.Login.register.utils;

public class RegisterTools : IRegisterTools
{
    /// <summary>
    /// 注册校验工具类
    /// </summary>
    public RegisterTools()
    {
    }


    public bool RegisterCheckout(NewRegisterUserInfo dto, out string str)
    {
        if (dto.Password?.Length is < 6 or > 20)
        {
            str = "Password below the standard";
            return false;
        }

        // if (dto.Sex is > 2 or < 1)
        // {
        //     str = "Sex below the standard ";
        //     return false;
        // }

        // if (dto.UserName?.Length is > 8 or < 1)
        // {
        //     str = "UserName below the standard ";
        //     return false;
        // }

        str = "Creation Successful";
        return true;
    }

    public string GenerateQqCode()
    {
        const long initiateNum = 1000000000;
        const long finishNum = 9999999999;
        var r = new Random();
        var resNum = r.NextInt64(initiateNum, finishNum);
        while (!QqCodeSqlNumCheckout(resNum))
        {
            resNum = r.NextInt64(initiateNum, finishNum);
        }

        return resNum.ToString();
    }

    private bool QqCodeSqlNumCheckout(long num)
    {
        using var db = new DataDbContext();
        var findNum = num.ToString();
        var data = db.Users.FirstOrDefault(e => e.Account == findNum);

        return data == null;
    }
}